// pages/store/index/index.js
var config = require('../../../config');
var strings = require('../../../strings.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //导航栏列表
    navList: [],
    curNav: 0,  //当前选定的导航栏下标
    //商品列表
    resultGoodsList:[],
    value: "", //搜索框，键盘输入的字符
    //banner 图片地址列表
    bannerImgUrls: [{
      id: 1,
      url: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg'
    }, {
      id: 2,
      url: 'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg'
    }, {
      id: 3,
      url: 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    }],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getPageFirstData()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  /**
   * 获取页面初始数据
   */
  getPageFirstData: function() {
    var that = this
    wx.request({
      url: config.getPowerFirstData,
      method: 'GET',
      success: function(res) {
        that.setData({
          navList: res.data.categorys,
          resultGoodsList: res.data.goodss,
          bannerImgUrls: res.data.images
        })
      }
    })
  },
  /**
   * 获取键盘输入
   */
  bindKeyInput: function (e) {
    console.log(e.detail.value)
    var value = e.detail.value
    this.setData({
      value
    })
  },
  /**
   * 搜索框点击搜索事件
   */
  searchTap: function() {
    var that = this
    if (that.data.value.length == 0) {
      wx.showToast({
        title: strings.isValueNull,
        icon: "none"
      })
      return
    }
    that.setData({
      resultGoodsList: [],
      currentTabID: ""
    })
    that.getSearchData()
  },
  
  /**
   * 获取搜索内容
   */
  getSearchData: function () {
    var that = this
    wx.request({
      url: config.powerSearchData,
      method: 'GET',
      // header:{},
      data: {
        goodsName: that.data.value,
        cid: that.data.currentTabID
      },
      success: function (res) {
        if(res.data.goodss.length == 0) {
          wx.showToast({
            title: strings.isSearchNull,
            icon: "none"
          })
        }
        that.setData({
          resultGoodsList: res.data.goodss
        })
      }
    })
  },
  /**
   * 导航栏选择点击事件
   */
  navTap: function(e) {
    var index = e.currentTarget.dataset.index
    console.log(index)
    this.setData({
      curNav: index
    })
    this.setData({
      value: "",
      resultGoodsList: [],
      currentTabID: this.data.navList[index].id
    })
    this.getSearchData()
  },
  //跳转到商品详情页
  goDetailsTap: function (e) {
    console.log(e)
    var id = e.currentTarget.dataset.id
    console.log(id)
    if (!id) {
      return
    }
    wx.navigateTo({
      url: '../details/index?id=' + id,
    })
  },
  //点击banner图片跳转至详情页
  bannerTap: function (e) {
    var id = e.currentTarget.dataset.id || null
    if (!id) {
      return
    }
    wx.navigateTo({
      url: '../details/index?id=' + id,
    })
  }
})